package org.shadow;

import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;

import java.awt.geom.Point2D;
import java.util.Arrays;

/**
 * ClassName: ApacheCommonsMath
 * Package: org.shadow
 * Description:
 *
 * @Author BuTang
 * @Create 2023/12/12 14:12
 * @Version 1.0
 */
public class vector {

    // 计算入射角度
    public static double calculateIncidentAngle(double[] targetPoint, double[] mirrorCenterPoint, double[] groundNormal) {
        double[] incidentRay = new double[] { targetPoint[0] - mirrorCenterPoint[0], targetPoint[1] - mirrorCenterPoint[1], targetPoint[2] - mirrorCenterPoint[2] };
        double incidentAngle = Math.acos(dotProduct(groundNormal, incidentRay) / (magnitude(groundNormal) * magnitude(incidentRay)));
        return incidentAngle;
    }

    // 计算反射光线的方向向量
    public static double[] calculateReflectionRay(double[] incidentRay, double[] groundNormal) {
        double dotProduct = 2 * dotProduct(incidentRay, groundNormal);
        double[] reflectionRay = new double[] { incidentRay[0] - dotProduct * groundNormal[0], incidentRay[1] - dotProduct * groundNormal[1], incidentRay[2] - dotProduct * groundNormal[2] };
        return reflectionRay;
    }

    // 计算向量的点积
    public static double dotProduct(double[] vector1, double[] vector2) {
        double result = 0;
        for (int i = 0; i < vector1.length; i++) {
            result += vector1[i] * vector2[i];
        }
        return result;
    }

    // 计算向量的模长
    public static double magnitude(double[] vector) {
        double sum = 0;
        for (double value : vector) {
            sum += value * value;
        }
        return Math.sqrt(sum);
    }

    public static void main(String[] args) {
        // 示例坐标
        double[] targetPoint = { 1, 1, 0 };
        double[] mirrorCenterPoint = { 0, 0, 0 };
        double[] groundNormal = { -1, 1, 0 };

        // 计算入射角度
        double incidentAngle = calculateIncidentAngle(targetPoint, mirrorCenterPoint, groundNormal);
        System.out.println("入射角度：" + Math.toDegrees(incidentAngle));

        // 计算反射光线的方向向量
        double[] incidentRay = { targetPoint[0] - mirrorCenterPoint[0], targetPoint[1] - mirrorCenterPoint[1], targetPoint[2] - mirrorCenterPoint[2] };
        double[] reflectionRay = calculateReflectionRay(incidentRay, groundNormal);
        System.out.println("反射光线的方向向量：" + reflectionRay[0] + ", " + reflectionRay[1] + ", " + reflectionRay[2]);
    }
}
